#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
import gen_md5
import fileutils

def useage():
    print ('python remove.py source_dir target_dir')
    pass

# 删除目标文件
def remove(source, target):
    if not os.path.isdir(source):
        raise Exception ('%s is not exist .' % source)
    if not os.path.isdir(target):
        raise Exception ('%s is not exist .' % target)
    is_make = gen_md5.make(source, source)
    if not is_make:
        raise Exception ('%s make md5.json failed .' % source)
    is_make = gen_md5.make(target, target)
    if not is_make:
        raise Exception ('%s make md5.json failed .' % target)
    source_md5 = gen_md5.readmd5(source)
    target_md5 = gen_md5.readmd5(target)
    for k in target_md5:
        if k in source_md5:
            __remove(source, k)
            pass
    pass

def __remove(root, file):
    root = fileutils.separator(root)
    new_file = fileutils.separator(file.encode('raw_unicode_escape'))
    fullpath = root + new_file
    if not os.path.isfile(fullpath):
        return
    fileutils.rmfile(fullpath)
    print ('remove -> %s' % file)
    pass

if __name__ == '__main__':
    try:
        sys.argv.append('D:\\git\\sszg_new\\client\\res')
        sys.argv.append('E:\\sszg_tmp\\res20210925gaidong\\20211008\\res_diff')
        argv = sys.argv
        arg_len = len(argv)
        if arg_len <= 2:
            useage()
            raise Exception('arg_len [%d] is error .' % arg_len)
        source = sys.argv[1]
        target = sys.argv[2]
        remove(source, target)
    except Exception as e:
        print (e)
        pass
    pass